//
//  LYFTabBar.swift
//  weibohhhh
//
//  Created by fersh on 16/1/2.
//  Copyright © 2016年 fersh. All rights reserved.
//

import UIKit

class LYFTabBar: UITabBar {
    
    var composeButtonClickBlock: (()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
       //添加加号按钮
        addSubview(composeButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
            
            composeButton.center.x = bounds.size.width * 0.5
            composeButton.center.y = bounds.size.height * 0.5
            let btnWid = bounds.size.width * 0.2
            var Index = 0
        for value in subviews {
//        
          if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            
                value.frame.size.width = btnWid
                value.frame.origin.x = btnWid * CGFloat(Index)
//                
                Index++
            
                if Index == 2 {
                    Index++
                }
            }
        }
    }
    
   @objc private func composeButtonClick() {
        
               
        composeButtonClickBlock?()
    
    }
    //懒加载按钮
    private lazy var composeButton: UIButton = {
        
        let button = UIButton()
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //设置按钮上的图片
         button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)

      button.sizeToFit()
      return button
    }()
    
}
